﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class RangeLengthValidatorAttribute : BaseValidatorAttribute
    {
        public RangeLengthValidatorAttribute(int minLength, int maxLength) : this(minLength, maxLength, ValidationUtils.Messages.Rangelength)
        {
        }
        public RangeLengthValidatorAttribute(int minLength, int maxLength, string errorMessage) : base(errorMessage)
        {
            MinLength = minLength;
            MaxLength = maxLength;
        }

        public int MaxLength { get; protected set; }

        public int MinLength { get; protected set; }

        public override bool IsValid(object value)
        {
            if (value == null) return false;
            var len = ValidationUtils.GetValueLenth(value);
            if (len == null) return true;
            return MinLength <= len && len <= MaxLength;
        }
        public override string FormatErrorMessage()
        {
            return string.Format(ErrorMessage, MinLength, MaxLength);
        }
    }
}
